<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_mitra_nonaktif extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_mitra_nonaktif_model', 'Mitra');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Mitra Non-Aktif";
		viewByTemplate('pro_mitra_nonaktif/index', $data, 'grid');
	}


	/*function create()
	{
		// Display Form Create
		$data['title'] = "Pemohon Mitra";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";
		$query = $this->db->query("SELECT kode_mitra FROM (SELECT * FROM pro_mitra ORDER BY kode_mitra DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_mitra'];
		else
			$MaxID = "MT0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['kode_mitra'] = "TS" . sprintf("%07s",$NextID);
		viewByTemplate('pro_mitra/form', $data, 'form');
	}*/

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Mitra Non-Aktif";
		$data['data'] = $this->Mitra->GetSingleView(get('kode_mitra'));
		$data['id_mitra_edit']=get('id_mitra');
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('pro_mitra_nonaktif/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Mitra";
		$data['data'] = $this->Mitra->GetSingleView(get('kode_mitra'));
		
		$data['mode'] = "view";
		viewByTemplate('pro_mitra/form', $data, 'form');
	}

	function show()
	{
		// Display Form View
		$data['title'] = "View Data Mitra";
		$data['data'] = $this->Mitra->GetSingleView(get('kode_mitra'));
		
		$data['mode'] = "show";
		viewByTemplate('pro_mitra/form', $data, 'form');
	}

	function getdata()
	{
		$this->secure_session();
		echo $this->Mitra->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation

		//Query Check
		$check = $this->db->query("SELECT ktp AS check_kode FROM pro_mitra WHERE id_mitra= '".$this->input->post("id_mitra")."'");
		$result = $check->row_array();

		//var_dump($result['check_kode']);

		$check1 = $this->db->query("SELECT siup AS check_kode FROM pro_mitra WHERE id_mitra= '".$this->input->post("id_mitra")."'");
		$result1 = $check1->row_array();

		$check2 = $this->db->query("SELECT situ AS check_kode FROM pro_mitra WHERE id_mitra= '".$this->input->post("id_mitra")."'");
		$result2 = $check2->row_array();

		$check3 = $this->db->query("SELECT npwp AS check_kode FROM pro_mitra WHERE id_mitra= '".$this->input->post("id_mitra")."'");
		$result3 = $check3->row_array();

		$check4 = $this->db->query("SELECT no_kontrak AS check_kode FROM pro_mitra WHERE id_mitra= '".$this->input->post("id_mitra")."'");
		$result4 = $check4->row_array();
		
		if (post("ktp") == '') {
			$error->Add("ktp", "No KTP", $tpl_empty);
		} else if (post("mode")=="edit" && post("ktp")!=$result['check_kode']){
			$check_edit = $this->db->query("SELECT COUNT(1) AS check_kode FROM pro_mitra WHERE ktp= '".$this->input->post("ktp")."'");
			$result_edit = $check_edit->row_array();
			if (post("mode") == "edit" && $result_edit['check_kode']!='0') {
				$error->Add("ktp", "No KTP", $tpl_duplicate);
			}
		} else if (post("mode")=="edit"&& post("situ")!=$result2['check_kode']){
			$check_edit2 = $this->db->query("SELECT COUNT(1) AS check_kode FROM pro_mitra WHERE situ= '".$this->input->post("situ")."'");
			$result_edit2 = $check_edit2->row_array();
			if (post("mode") == "edit" && $result_edit2['check_kode']!='0') {
				$error->Add("situ", "No SITU", $tpl_duplicate);
			}
		} else if (post("mode")=="edit"&& post("siup")!=$result1['check_kode']){
			$check_edit1 = $this->db->query("SELECT COUNT(1) AS check_kode FROM pro_mitra WHERE siup= '".$this->input->post("siup")."'");
			$result_edit1 = $check_edit1->row_array();
			if (post("mode") == "edit" && $result_edit1['check_kode']!='0') {
				$error->Add("siup", "No SIUP", $tpl_duplicate);
			}
		} else if (post("mode")=="edit"&& post("npwp")!=$result3['check_kode']){
			$check_edit3 = $this->db->query("SELECT COUNT(1) AS check_kode FROM pro_mitra WHERE npwp= '".$this->input->post("npwp")."'");
			$result_edit3 = $check_edit3->row_array();
			if (post("mode") == "edit" && $result_edit3['check_kode']!='0') {
				$error->Add("npwp", "No NPWP", $tpl_duplicate);
			}
		} else if (post("mode")=="edit"&& post("no_kontrak")!=$result4['check_kode']){
			$check_edit4 = $this->db->query("SELECT COUNT(1) AS check_kode FROM pro_mitra WHERE no_kontrak= '".$this->input->post("no_kontrak")."'");
			$result_edit4 = $check_edit4->row_array();
			if (post("mode") == "edit" && $result_edit4['check_kode']!='0') {
				$error->Add("no_kontrak", "No KTP", $tpl_duplicate);
			}
		} else if (post("nama_pemilik") == '') {
			$error->Add("nama_pemilik", "Nama Pemilik", $tpl_empty);
		} else if (post("nama_toko_mitra") == '') {
			$error->Add("nama_toko_mitra", "Nama Pemilik", $tpl_empty);
		} else if (post("alamat_lengkap") == '') {
			$error->Add("alamat_lengkap", "Alamat Lengkap", $tpl_empty);
		}




		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{


				// Fill Post Data
				$data = array(
							'kode_mitra' => $this->input->post('kode_mitra'),
							'ktp' => $this->input->post('ktp'),
							'situ' => $this->input->post('situ'),
							'siup' => $this->input->post('siup'),
							'npwp' => $this->input->post('npwp'),
							'nama_toko_mitra' => $this->input->post('nama_toko_mitra'),
							'alamat_lengkap' => $this->input->post('alamat_lengkap'),
							'no_telp' => $this->input->post('no_telp'),
							'no_hand' => $this->input->post('no_hand'),
							'status_mitra' => $this->input->post('status_mitra'),
							'no_kontrak' => $this->input->post('no_kontrak'),
							'awal_kerja' => $this->input->post('awal_kerja'),
							'akhir_kerja' => $this->input->post('akhir_kerja'),
							'fee_mitra' => $this->input->post('fee-mitra'),
							'no_rekening' => $this->input->post('no_rekening'),
							'nama_bank' => $this->input->post('nama_bank'),
							'atas_nama' => $this->input->post('atas_nama'),
							'nama_pemilik' => $this->input->post('nama_pemilik')
							);

				$data['awal_kerja']			= ($data['awal_kerja'] == '' ? NULL : date_format3($data['awal_kerja'], 'YYYY-MM-DD'));
				$data['akhir_kerja']		= ($data['akhir_kerja'] == '' ? NULL : date_format3($data['akhir_kerja'], 'YYYY-MM-DD'));

				if (post("mode") == "create")
				{
					$this->Mitra->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Mitra->Update($data, array('kode_mitra'	=> $data['kode_mitra']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$kode_mitra	= get('kode_mitra');
		
		if ($kode_mitra != "")
		{
			// Delete Single
			$arr_rule	= array('kode_mitra' => $kode_mitra);
			$ls_result = $this->Mitra->DeleteSingle($arr_rule);

		}
		else
		{
		}
		
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kode_mitra' => $row['kode_mitra']);
				$ls_result .= $this->Mitra->DeleteSingle($arr_rule);
			}
		echo $ls_result;
	}
}
?>